﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace PDX
{
    /// <summary>
    /// Extension methods
    /// </summary>
    public static class ExtensionMethods
    {
        /// <summary>
        /// Gets the one and only attribute of type T, or null.
        /// </summary>
        /// <typeparam name="T">The type of attribute to get</typeparam>
        /// <param name="property">The property.</param>
        /// <returns></returns>
        public static T GetSingleAttribute<T>(this MemberInfo property) where T : Attribute
        {
            var atts = property.GetCustomAttributes(typeof(T), true);
            if (atts.Length == 1)
            {
                return atts[0] as T;
            }
            return null;
        }

        /// <summary>
        /// Determines whether the specified member has
        /// one and only one attribute of type T.
        /// </summary>
        /// <typeparam name="T">The type of attribute to get</typeparam>
        /// <param name="member">The member.</param>
        /// <returns>
        ///   <c>true</c> if the specified member has attribute; otherwise, <c>false</c>.
        /// </returns>
        public static bool HasSingleAttribute<T>(this MemberInfo member) where T : Attribute
        {
            var att = member.GetSingleAttribute<T>();
            return att != null;
        }
    }
}
